
integer attachment = TRUE;
integer relaychannel = -1812221819;
string cmdname;
key victim;
list gMenu=[];
list statesMenu=[0,0,0,0,0,0,0,0,0,0];
list gAvatarAllow;
integer gMenuLink =1337;
integer gDemon =1338;
key ToucherID;
integer channel_dialog;
integer listen_id;
integer listen_id2;
integer channel_Update=1336;
string msg ="Update to 0.11 Beta";
integer gPlugin =1339;

integer pin = 15385;
list gTimerMenuL =["+5 Min","+10 Min","+30 Min","+1 H","+5 H","+10 H","+1 D","Stop","Reset","Start","Back"];
integer gTimeleft=0;
integer gTimerMenu=0;
integer GmenuTimer;
integer GLockTimer;
string msgTimer ="Set a Time the Sub get's locked";

string getHoursMinutes(integer seconds)
{
	integer gsec = seconds;
	integer hours = 0;
	integer minutes = 0;
	integer months = 0;
	integer days = 0;
	integer years = 0;
	string returnValue="";
	years=seconds/31536000;
	seconds=seconds - (years*31536000);
	months=seconds/2628000;
	seconds=seconds - (months*2628000); 
	days=seconds/86400;
	seconds=seconds - (days*86400);
	hours=seconds/3600;
	seconds=seconds - (hours*3600);
	minutes=seconds/60;
	seconds=seconds - (minutes*60);
	if (years) returnValue= (string)years + "Yr. ";
	if (days) returnValue= returnValue +(string)days + "Day. ";
	if (hours) returnValue= returnValue +(string)hours + "Hr. ";
	if (minutes) returnValue= returnValue +(string)minutes + "Min. ";
	if (seconds) returnValue= returnValue + (string)seconds + "Sec.";
	//returnValue= returnValue +"\n"+ (string)gsec + "Time.";
	return (returnValue);
}

makeMenu()
{
	gMenu=[];
	//gMenu+=sideMenu;
	gMenu+=gTimerMenuL;

}

displayMenu()
{
	channel_dialog = ( -1 * (integer)("0x"+llGetSubString((string)llGetKey(),-5,-1)) );
	listen_id = llListen( channel_dialog, "", NULL_KEY, "");
	msgTimer="Time left:"+getHoursMinutes(gTimeleft);
	llDialog(ToucherID, msgTimer,gMenu, channel_dialog);
}

startTimer()
{
	GLockTimer=TRUE;
}

endTimer()
{
	gTimeleft=0;
	GLockTimer=FALSE;
	llMessageLinked(LINK_THIS, gPlugin, "RESET", ToucherID);
}

stoptimer()
{
	GLockTimer=FALSE;
}

setTimer(integer Min)
{
	
	gTimeleft+=Min*60;//gTimeleft=Min*2*60;
	msgTimer="Time left:"+getHoursMinutes(gTimeleft);
}

endMenu()
{
	llListenRemove(listen_id);
}

default {
    state_entry() 
    {
   		llSetTimerEvent(1.0);
   		makeMenu();
   		
    }
    
    listen(integer channel, string name, key id, string message)
    {
		if(channel==channel_dialog)
		{
    		llListenRemove(listen_id);
    		if(message=="+5 Min")
		    {
		        setTimer(5);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;
		    }
		    else if(message=="+10 Min")
		    {
		        setTimer(10);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;    
		    }
		    else if(message=="+30 Min")
		    {
		        setTimer(30);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;    
		    }
		    else if(message=="+1 H")
		    {
		        setTimer(60);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;    
		    }
		    else if(message=="+5 H")
		    {
		        setTimer(300);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;    
		    }
		    else if(message=="+10 H")
		    {
		        setTimer(600);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;    
		    }
		    else if(message=="+1 D")
		    {
		        setTimer(1440);
		    	displayMenu();
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;    
		    }
		    //"Stop","Reset","Start",
		    else if(message=="Stop")
		    {
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;
		    	stoptimer();
		    	displayMenu();
		    }
		    else if(message=="Reset")
		    {
		    	gTimeleft=0;
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;
		    	stoptimer();
		    	displayMenu();
		    }
		    else if(message=="Start")
		    {
		    	gTimerMenu=45;
		    	gTimerMenu=TRUE;
		    	startTimer();
		    	displayMenu();
		    }
			if(message=="Back")llMessageLinked(LINK_THIS, gMenuLink, "DV_BASIC", ToucherID);
		}
    }
    
    on_rez(integer on)
    {
    
    }
    
    link_message(integer sender, integer num, string str, key id)
  	{
  		
  		if(num ==gMenuLink)
  		{
  			if(str==llGetScriptName())
  			{
  				llMessageLinked(LINK_THIS, gDemon, llGetScriptName(), NULL_KEY);
  				ToucherID=id;
  				displayMenu();
  			}
  		}
  		if(num ==gDemon)
  		{
  			if(str =="attachment")attachment=TRUE;
  			else if(str =="device")attachment=FALSE;
  			else if(str!=llGetScriptName())
  			{
				llListenRemove(listen_id);
  			}
  		}
  		if(num==gPlugin)
  		{
  			if(str=="ON_REZ")
  			{
  				
  			}
  			if(str=="RESET")
  			{
  				gTimeleft=0;
				GLockTimer=FALSE;
  			}
  		}
  	}
  	
  	timer() 
	{
		if(GLockTimer==TRUE)
		{
			gTimeleft=gTimeleft-1;
			if(gTimeleft==0)
			{
				endTimer();
			}
		}
		if(GmenuTimer==TRUE)
		{
			gTimerMenu=gTimerMenu-1;
			if(gTimerMenu==0)
			{
				endMenu();
			}			
		}
	}
}
